package de.tapps.brainstorming;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.util.Log;
/**
* Deletes an Answer or a topic from the database
* @author Timo
* Encrypted - ok
*/
public class DeleteEntry extends AsyncTask<String, Integer, String> {
	int topicID;
	int answerID;
	String imei;
	private Crypter crypter;
	private MainActivity activity = new MainActivity();;

	public DeleteEntry(MainActivity act, int ptopicID, int panswerID, String pimei) {
		imei = pimei;
		topicID = ptopicID;
		answerID = panswerID;
		activity = act;
		crypter = new Crypter(activity);
		

	}

	@Override
	protected String doInBackground(String... arg0) {
		new Thread(new Runnable() {
			public void run() {
				// Create a new HttpClient and Post Header
				HttpClient httpclient = new DefaultHttpClient();
				HttpPost httppost = new HttpPost(
						"http://t-apps-online.de/BrainStorming/deleteEntry.php");
				try {
					
					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
							2);
					nameValuePairs.add(new BasicNameValuePair("topicid", 
							""+topicID));
					nameValuePairs.add(new BasicNameValuePair("answerid",
							""+answerID));
					
					nameValuePairs
							.add(new BasicNameValuePair("imei",crypter.encrypt(imei)));
							
					httppost.setEntity(new UrlEncodedFormEntity(
							nameValuePairs));
					// Execute HTTP Post Request
					HttpResponse response = httpclient.execute(httppost);
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
				} catch (IOException e) {
					// TODO Auto-generated catch block
				}
			}
		}).start();
		return null;
	}
}
